# -*- coding:utf-8 -*-

content = """<test>
	"main"
	<a1   key1="hoolai1" key2="company1"/>
	<name1 key1="super1">"cctv1"</name1>
	<int1>666
		</int1>

	<second>
		<a2   key1="hoolai2" key2="company2"/>
			<name2 key1="super2">"cctv2"</name2>
		<int2>777
		</int2>
	</second>
	<char1> "c" </char1>   
</test>"""

# content = """<bookstore>
#     <book category="cooking">
#         <title lang="en">Everyday Italian</title>
#         <author>Giada De Laurentiis</author>
#         <year>2005</year>
#         <price>30.00</price>
#     </book>
#     <book category="children">
#         <title lang="en">Harry Potter</title>
#         <author>J.K. Rowling</author>
#         <year>2005</year>
#         <price>29.99</price>
#     </book>
# </bookstore>
# """

content = """
<bookstore>
    <book category="cooking">
        <title>Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
    </book>
    <book category="children">
        <title>Harry Potter</title>
        <author>J.K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>
</bookstore>
"""

content = """
<data>
    <person id="1">
        <name>Person 1</name>
        <age>21</age>
        <city>City 1</city>
        <pets>
            <pet id="1">Pet 1 of Person 1</pet>
            <pet id="2">Pet 2 of Person 1</pet>
            <pet id="3">Pet 3 of Person 1</pet>
        </pets>
    </person>
    <person id="2">
        <name>Person 2</name>
        <age>22</age>
        <city>City 2</city>
        <pets>
            <pet id="1">Pet 1 of Person 2</pet>
            <pet id="2">Pet 2 of Person 2</pet>
            <pet id="3">Pet 3 of Person 2</pet>
        </pets>
    </person>
</data>
"""


class XMLParser:
    def __init__(self, content):
        self.content = content
        self.pos = 0
    
    def dump(self):
        tree = self._parse()
        output = []
        self._dump_node(tree, output, 0)
        print("\n".join(output))
        
    def _dump_node(self, node, output, indent):
        line = ""
        if isinstance(node, str):
            line += "|  " * (indent - 1) + "|-- " + node
            output.append(line)
            return
        
        if indent == 0:
            line = node[0]
        else:
            line = "|  " * (indent - 1) + "|-- " + node[0]
        
        if len(node[1]) > 0:
            line += " ("
            for k, v in node[1].items():
                line += k + ": " + v + ", "
            line = line[:-2]
            line += ")"
        
        if len(node[2]) == 0:
            output.append(line)
            return
        
        if len(node[2]) == 1 and isinstance(node[2][0], str):
            line += ": " + node[2][0]   
            output.append(line)
            return
        
        output.append(line)
        for i in range(len(node[2])):
            self._dump_node(node[2][i], output, indent + 1)
            
    
    def _parse(self):
        return self._parse_element()

    def _parse_element(self):
        tag = self._consume_tag()
        attributes = self._parse_attributes()
        children = []
        while not self._is_eof():
            self._lstrip()
            if self._start("/>") or self._start("</"):
                self._consume_util(">")
                self._consume(">")
                break
            if self._start("<"):
                children.append(self._parse_element())
            else:
                children.append(self._parse_text())
        return (tag, attributes, children)
    
    def _parse_attributes(self):
        self._lstrip()
        attributes = {}
        while not self._start(">") and not self._start("/>"):
            key = self._consume_util("=")
            self._consume("=")
            self._consume_util("\"'")
            quot = self.content[self.pos]
            self._consume(quot)
            value = self._consume_util(quot)
            self._consume(quot)
            attributes[key.strip()] = value 
        if self._start(">"):
            self._consume(">")
        return attributes
    
    def _parse_text(self):
        self._lstrip()
        text = self._consume_util("<")
        text = text.strip()
        text = text[1:-1]
        return text
        

    def _consume_tag(self):
        self._lstrip()
        self._consume("<")
        return self._consume_util(" >")

    def _consume(self, char):
        assert self.content[self.pos] == char, "parse error at: \n" + str(self.content[self.pos:])
        self.pos += 1
        return
    
    def _consume_util(self, chars):
        beg = self.pos
        while self.content[self.pos] not in chars:
            self.pos += 1
        return self.content[beg: self.pos]
    
    def _start(self, match):
        for i in range(len(match)):
            if self.content[self.pos + i] != match[i]:
                return False
        return True
    
    def _is_eof(self):
        return self.pos >= len(self.content)
    
    def _lstrip(self):
        while self.content[self.pos] in " \n\r\t":
            self.pos += 1

xml = XMLParser(content)
xml.dump()